//
//  HHScatterViewController.m
//  Flower
//
//  Created by 花花 on 2016/12/5.
//  Copyright © 2016年 花花. All rights reserved.
//

#import "HHScatterViewController.h"

@interface HHScatterViewController (){

    int totalsactterbtn;
    
    CGFloat ang;
    CGFloat centerDis;
    NSMutableArray *btnArray;
    CGPoint startPoint;
    
}

@property (weak, nonatomic)  UIButton *btn;
@end

@implementation HHScatterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor =[UIColor whiteColor];
    UIButton *btn =[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 80, 80)];
    btn.center = self.view.center;
    NSLog(@"%@",NSStringFromCGRect(btn.frame));
    [btn setImage:[UIImage imageNamed:@"Smile"] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [btn addGestureRecognizer:   [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(GestAction:)]];
    [self.view addSubview:btn];
    _btn = btn;

    [self createsactterBtnWithTotal:7 gap:10 centenrDistance:90];
  }


-(void)createsactterBtnWithTotal:(int)total gap:(CGFloat)gap centenrDistance:(CGFloat)distance{
    for (int i = 0; i<total; i++) {
        CGFloat angle = M_PI * 2 / total;
        CGFloat rad = distance * sinf(angle / 2) - gap / 2.0;
        ang = angle;
        centerDis = distance;
        totalsactterbtn  = total;
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        CGRect rect = { {0,0},{2 * rad, 2 * rad} };
        btn.frame = rect;
        btn.tag = 100 + i;
        btn.alpha = 0;
        btn.layer.cornerRadius = rad;
        [btn setTitle:[NSString stringWithFormat:@"第%d个",i] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor purpleColor] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor brownColor] forState:UIControlStateSelected];
        [btn setBackgroundImage:[UIImage imageNamed:@"bg1"] forState:UIControlStateNormal];
        [btn setBackgroundImage:[UIImage imageNamed:@"Smile-1"] forState:UIControlStateSelected];
        [btn addTarget:self action:@selector(sactterbtnClick:) forControlEvents:UIControlEventTouchUpInside];
        btn.center = self.view.center;
        [self.view addSubview:btn];
        btnArray = [NSMutableArray array];
        [btnArray addObject:btn];

    }
}
-(void)sactterbtnClick:(UIButton *)btn{
    
    NSLog(@"%@",@(btn.tag));

    for (int i = 0; i<totalsactterbtn; i++) {
        if (i==btn.tag -100) {
            btn.selected = YES;
        }else{
            UIButton *button =(UIButton *)[self.view viewWithTag:i+100];
            button.selected = NO;
        }
    }
}

- (void)btnClick:(UIButton *)sender {
    
    for (int i = 0; i < totalsactterbtn; i ++) {
        UIButton *btn = (UIButton *)[self.view viewWithTag:100 + i];
        CGFloat x = centerDis * cosf(ang * i) + sender.center.x;
        CGFloat y = centerDis * sinf(ang * i) + sender.center.y;
        [UIView animateWithDuration:0.3 delay:0.1 * i options:UIViewAnimationOptionCurveLinear animations:^{
            if (!CGPointEqualToPoint(btn.center, sender.center)) {
                UIButton *btn = (UIButton *)[self.view viewWithTag:100 + i];
                btn.center = sender.center;
                btn.alpha = 0;
                sender.selected = NO;
            }
            else {
                btn.center = CGPointMake(x, y);
                btn.alpha = 1.0;
                sender.selected = YES;
            }
        } completion:nil];
    }
}

- (void)GestAction:(UIPanGestureRecognizer *)sender{
    
    CGPoint point = [sender translationInView:self.view];
    sender.view.center = CGPointMake(sender.view.center.x + point.x, sender.view.center.y + point.y);
    [sender setTranslation:CGPointZero inView:self.view];
    UIButton *centerBtn = (UIButton *)sender.view;
    for (int i = 0; i < totalsactterbtn; i ++) {
        UIButton *btn = (UIButton *)[self.view viewWithTag:100 + i];
        CGFloat x = centerDis * cosf(ang * i) + centerBtn.center.x;
        CGFloat y = centerDis * sinf(ang * i) + centerBtn.center.y;
        if (centerBtn.selected) {
            btn.center = CGPointMake(x, y);
        }
        else {
            btn.center = centerBtn.center;
        }
    }
    
}

@end
